package LeetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LC_609_FindDuplicateFileinSystem {

    public static void main(String[] args) {

    }

    public class Solution {
        public List<List<String>> findDuplicate(String[] paths) {
            Map<String, ArrayList<String>> store = new HashMap<>();
            for (String temp : paths) {
                String[] data = temp.split(" ");
                String path = data[0];
                for (int i = 1; i < data.length; ++i) {
                    String file = data[i];
                    StringBuilder newpath = new StringBuilder(path);
                    newpath.append("/");
                    int j = 0;
                    for (j = 0; j < file.length(); ++j) {
                        if (file.charAt(j) != '(')
                            newpath.append(file.charAt(j));
                        else
                            break;
                    }
                    String context = file.substring(j + 1, file.length() - 1);
                    ArrayList<String> al = store.getOrDefault(context, new ArrayList<>());
                    al.add(newpath.toString());
                    store.put(context, al);
                }
            }
            List<List<String>> re = new ArrayList<>();
            for (ArrayList<String> al : store.values())
                if (al.size() > 1)
                    re.add(al);
            return re;
        }
    }

}